let Sequelize = require("sequelize");

module.exports = function(sequelize, DataTypes) {
	let User = sequelize.define("User", {
		id: { type: Sequelize.INTEGER, primaryKey: true,autoIncrement: true },
		fullname: { type: Sequelize.STRING},
		nickname: { type: Sequelize.STRING },
		sex :  { type: Sequelize.INTEGER},
		mobile :  { type: Sequelize.STRING},
		password :  { type: Sequelize.STRING},
		status :  { type: Sequelize.INTEGER},
		headimg :  { type: Sequelize.STRING},
		corpid :   { type: Sequelize.STRING},
		vip : { type: Sequelize.INTEGER},
		extra : {type:Sequelize.TEXT}
	}, {
		tableName: 'uc_user',
		comment: '用户基础表',
		underscored: true,
		'timestamps': true,
		'paranoid': true,
		'createdAt' : 'created_at',
		'updatedAt' : 'updated_at',
		'deletedAt' : 'deleted_at',
		//charset: 'utf8',
		//collate: 'utf8_general_ci',
		//charset: 'utf8mb4',
		//collate: 'utf8mb4_bin',
		classMethods: {
			associate: function(models) {
				User.belongsTo(models.Corp, {
					onDelete: "CASCADE",
					foreignKey: {
						name : 'corpid',
						allowNull: true
					}
				});
				User.belongsToMany(models.Role, { through: models.UserRole,foreignKey: 'uid', })
			}
		},
		instanceMethods: {
			toJSON: function () {
				let values = Object.assign({}, this.get());
				values.password  = !!values.password
				// delete values.password;
				delete values.deleteAt;
				return values;
			}
		}
	});
	return User;
};
